import {defineStore} from 'pinia'
import {useProgressConfigStore} from '@/stores/ProgressConfig'
import router from '@/router'

export const usePMConfig = defineStore('PMConfig', {
    state: () => {
        return {
            projectId: 0,
            name: '',
            head: '',
            startDate: new Date(),
            endDate: new Date(),
            date: [],
            isChangeDate: false,
            difference: 0
        }
    },
    getters: {
        getName(state) {
            return state.name
        },
        getHead(state) {
            return state.head
        }
    },
    actions: {
        clearAll() {
            this.$patch({
                projectId: 0,
                name: '',
                head: '',
                startDate: new Date(),
                endDate: new Date(),
                date: [],
                isChangeDate: false,
                difference: 0
            })
        },
        setId(id) {
            this.projectId = id
        },
        getMonthDifference() {
            const startMonth = this.startDate.getFullYear() * 12 + this.startDate.getMonth()
            const endMonth = this.endDate.getFullYear() * 12 + this.endDate.getMonth()
            this.difference = (endMonth - startMonth)
            return (endMonth - startMonth)
        },
        pushDate() {
            useProgressConfigStore().deleteData()
            for (let i = 0; i < this.getMonthDifference(); i++) {
                if (this.startDate.getMonth() > 11) this.startDate.setMonth(0)
                useProgressConfigStore().tabledata.push({
                    date: this.startDate.getFullYear().toString() + '-' + (this.startDate.getMonth() + 1).toString(),
                    reality: 0,
                    show: true,
                    plan: 0
                })
                this.startDate.setMonth(this.startDate.getMonth() + 1)
            }
            router.push('/home/programmanagement/newconfig/2')
        },
        returnDate() {
            if (!this.isChangeDate) {
                for (let i = 0; i < this.getMonthDifference(); i++) {
                    this.startDate.setMonth(this.startDate.getMonth() - 1)
                }
            }
            this.isChangeDate = true
            return this.startDate
        },
        returnDateOne() {
            for (let i = 0; i < this.getMonthDifference(); i++) {
                this.startDate.setMonth(this.startDate.getMonth() - 1)
            }
        }
    }

})
